import { G } from '../common'
cc.Class({
    extends: cc.Component,

    properties: {
        RankNode: cc.Node,
        AuthButton: cc.Node,
    },

    onLoad () {
       this.applyUserAuth();
        //预加载场景
        cc.director.preloadScene("game", function () {});
    },

    /**
     * 请求授权信息
     */
    applyUserAuth(){
        console.log("请求授权信息");
        let self = this;
        let wx = window['wx'];
        wx.login({
            success:function(res){
                console.log(res);
                let code = res.code;
                wx.getSetting({success (res) {
                                //用户已经授权
                                if (res.authSetting["scope.userInfo"]) {
                                        wx.getUserInfo({
                                            success(res){
                                                console.log("已经授权")
                                                console.log(res)
                                                G.is_login = true;
                                            // self.DrawUserInfo(res.userInfo); //画用户信息(微信的信息)
                                            // self.GetToken(res,code);//获取TOKEN（请求API的信息）
                                            }
                                        });
                                }else{
                                    console.log("未授权")
                                }
                    }
                })
            }
        })
    },
    /**
     * 创建点击授权按钮
     * @param {*} btnNode 
     */
    createAuthorizeBtn(btnNode) {
        let btnSize = cc.size(btnNode.width+10,btnNode.height+10);
        let frameSize = cc.view.getFrameSize();
        let winSize = cc.director.getWinSize();
        //适配不同机型来创建微信授权按钮
        let left = (winSize.width*0.5+btnNode.x-btnSize.width*0.5)/winSize.width*frameSize.width;
        let top = (winSize.height*0.5-btnNode.y-btnSize.height*0.5)/winSize.height*frameSize.height;
        let width = btnSize.width/winSize.width*frameSize.width;
        let height = btnSize.height/winSize.height*frameSize.height;
        let self = this;
        self.btnAuthorize = wx.createUserInfoButton({
            type: 'text',
            text: '',
            style: {
                left: left,
                top: top,
                width: width,
                height: height,
                lineHeight: 0,
                backgroundColor: '',
                color: '#ffffff',
                textAlign: 'center',
                fontSize: 16,
                borderRadius: 4
            }
        })
    
        self.btnAuthorize.onTap((uinfo) => {
            console.log("onTap uinfo: ",uinfo);
            if (uinfo.userInfo) {
                console.log("wxLogin auth success");
                wx.showToast({title:"授权成功"});

                G.is_login = true;
            }else {
                console.log("wxLogin auth fail");
                wx.showToast({title:"授权失败",'icon':'error'});
            }
        });
    },

    OnclickBegin(){
        cc.director.loadScene("game");
    },

    OnclickRank(e,str){

        if(str == "open"){
            this.RankNode.active = true;

            if(!G.is_login){
                this.AuthButton.active = true;
                this.createAuthorizeBtn(this.AuthButton);
            }else{
                this.AuthButton.active = false;
            }
           
        }else{
            this.RankNode.active = false;
            this.btnAuthorize.destroy();
        }
       
    },

    OnclickSetting(){
        Alert.show("分享好友可得能量！", () => {
                    this.click()
        });
    },

    click () {
        console.log('alertttttt')
    },

});
